
let UIConstant = require('../ui2/ui_constants');
let animUtils = require('../ui2/anims/animUtils');

let State = require('./state');

// 查看
class ViewState extends State {
    onEnter() {
        super.onEnter();
        this.showDeco();
        this.showBattery();
        //
        let mapScene = editor.gameApp;
        mapScene.selectDeco(null);

        // 所有的动画都播放起来
        let lstDeco = editor.getDecoView().getModel();
        let actionMgr = cc.director.getActionManager();
        for (let i = 2, len = lstDeco.length; i < len; ++i) {
            let deco = lstDeco[i];
            actionMgr.removeActionByTag(G.Tags.DecoBaseAction,deco.node);
            if (deco.anims && deco.anims.tempId > 0) {
                // 创建动画
                let animType = deco.anims.tempId;
                let animData = deco.anims;
                if (animType === UIConstant.AnimTemplate.MoveBy) {
                    let time = animData.time;
                    let dx = animData.dx;
                    let dy = animData.dy;
                    let forever = animUtils.makeMoveBy(time, dx, dy);
                    deco.node.runAction(forever);
                } else if (animType === UIConstant.AnimTemplate.ScaleTo) {
                    let initSx = deco.node.scaleX;
                    let initSy = deco.node.scaleY;
                    let time = animData.time;
                    let sx = animData.dx;
                    let sy = animData.dy;
                    let forever = animUtils.makeScaleTo(time, initSx, initSy, sx, sy);
                    deco.node.runAction(forever);
                } else if (animType === UIConstant.AnimTemplate.RotateBy) {
                    let time = animData.time;
                    let angle = animData.dx;
                    let forever = animUtils.makeRotateBy(time, angle);
                    deco.node.runAction(forever);
                } else {
                    throw new Error(`unknow animType:${animType}`);
                }
            }
        }
    }
}

module.exports = ViewState;